package demo.android.com.mvpdemo.RxJava;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import demo.android.com.mvpdemo.R;

/**
 * Created by Vya on 2018/1/24 0024.
 */

public class TestRxjavaActivity extends AppCompatActivity {
    private ImageView mImage;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_rxjava_activity);
        mImage = (ImageView) findViewById(R.id.image);

        /*Observable.just("SFSDIF")
                .subscribe(new Observer<String>() {
                    @Override
                    public void onNext(@NonNull String s) {
                        Log.e("wangyan","onNext====>"+s);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("wangyan","onError--"+e.getMessage());
                    }

                    @Override
                    public void onSubscribe() {
                        Log.e("wangyan","onSubscribe--");
                    }

                    @Override
                    public void onComplete() {
                        Log.e("wangyan","onComplete--");
                    }
                });*/
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Observable.just("http://img.taopic.com/uploads/allimg/130331/240460-13033106243430.jpg")
                            .map(new Function<String,Bitmap>(){

                                @Override
                                public Bitmap apply(String urlPath) throws Exception{
                                    URL url = new URL(urlPath);
                                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                                    InputStream inputStream = urlConnection.getInputStream();
                                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                                    Log.e("wangyan","--------------111---------"+Thread.currentThread().getName());

                                    return bitmap;
                                }
                            })
                            .map(new Function<Bitmap, Bitmap>() {

                                @Override
                                public Bitmap apply(Bitmap bitmap) throws Exception {
                                    bitmap = createWatermark(bitmap, "RxJava2.0");
                                    Log.e("wangyan","--------------2222---------"+Thread.currentThread().getName());
                                    return bitmap;
                                }
                            })
                            .map(new Function<Bitmap, Bitmap>() {
                                @Override
                                public Bitmap apply(Bitmap bitmap) throws Exception {
                                    Log.e("wangyan","--------------33333---------"+Thread.currentThread().getName());
                                    return bitmap;
                                }
                            })
                            .subscribeOn(Schedulers.io())
                            .observerOn(Schedulers.mainThread())
                            .subscribe(new Consumer<Bitmap>() {
                                @Override
                                public void onNext(final Bitmap bitmap) {
                                    Log.e("wangyan","--------------4444---------"+Thread.currentThread().getName());
                                    // 第七步
                                    Log.e("wangyan", "item = " + bitmap);
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            mImage.setImageBitmap(bitmap);
                                        }
                                    });
                                }
                            },111);
                }
            }).start();
    }

    private Bitmap createWatermark(Bitmap bitmap, String mark) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint p = new Paint();
        // 水印颜色
        p.setColor(Color.parseColor("#C5FF0000"));
        // 水印字体大小
        p.setTextSize(150);
        //抗锯齿
        p.setAntiAlias(true);
        //绘制图像
        canvas.drawBitmap(bitmap, 0, 0, p);
        //绘制文字
        canvas.drawText(mark, 0, h / 2, p);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        return bmp;
    }

}
